import { spotMarketBatch, spotMarketCommon } from "@src/api/spotMarket"

import { AsyncRecursion, CbParams, creatAsyncRecursion } from '@src/utils/asyncRecursion'

import type { GetAllNodeDataParams, NodeMsg } from "./type"
import { getSpotMartetReqInfo } from "../../generate"
import { createSerialDates } from "@src/dataFormatConversion/tool"
import { getDataFromLocalStorage, promisetTimeOut, saveDataToLocalStorage, sdTime96, validateJSON } from "@src/utils/common"

import { cloneDeep } from "lodash";
import { DataSourceTypeEnum, DataSourceTypeByDateEnum } from "@src/globalData/enum";
import { yunNao } from "@src/api/yunNao";
import { dataCategory, getDefaultDataBaseData, sendParamsStructure } from "@src/dataFormatConversion/constant"
import dayjs from "dayjs";
import { sendParamFilterData } from "@src/dataFormatConversion/formatData"


/** 需要替换的ID数据 */
export const defaultNodeIdsData:[string,string][] = [['oldId','newId']]

/** 保存 需要替换的ID数据 */
export const saveReplaceNodeIdsData = (nodeIdsData:any) => {
  saveDataToLocalStorage(JSON.stringify(nodeIdsData), 'replaceNodeIdsData')
}
/** 获取 需要替换的ID数据 */
export const getReplaceNodeIdsData = async ():Promise<any> => {
  try {
    const nodeIdsData = await getDataFromLocalStorage('replaceNodeIdsData')
    
    if(!nodeIdsData) return Promise.resolve(cloneDeep(defaultNodeIdsData))

    return Promise.resolve(JSON.parse(nodeIdsData as string))
  }catch(e) {
    return Promise.resolve(cloneDeep(defaultNodeIdsData))
  }
}

/**
 * 获取 需要获取数据的list
 * @param isHandle 1. false: 获取120个节点的数据，在getOneNodeData中自动替换; 2. true: 只获取替换节点的数据，在getOneNodeData中自动替换
 * @returns 
 */
const getReplaceNodeData = async (isHandle = false) => {
  if(!isHandle) return Promise.resolve(nodesData)

  const replaceNodeIds = await getReplaceNodeIdsData() as [string,string][]

  if(!replaceNodeIds?.[0]?.length || String(replaceNodeIds) === String(defaultNodeIdsData)) return Promise.resolve(nodesData)

  const replaceNodeData = nodesData.filter(val => {
    const {nodeId} = val
    return !!replaceNodeIds.find(item => item[0] === nodeId)
  })

  return Promise.resolve(replaceNodeData)
}

// 校验是否可解析为 json
export const validateNodeIds = (rule: any, value: any, callback: any) => {
  if (value && !validateJSON(value)) {
    callback(new Error('输入json有误'))
  } else {
    callback()
  }
}

  /**
   * 【现货市场】-【现货交易】-【日前交易结果】-【一次日前节点电价查询】
   * 全网所有节点日前出清节点电价
   * D + 1
   * AHEAD_NODE_PRICE_INFO = "ahead_node_price_info"
   */
  /**
   * https://pmos.sd.sgcc.com.cn:18080/trade/main/home/changeDate.do?pdate=2024-03-19&_=1711692352927
   * 【现货市场】-【现货交易】-【实时交易结果】-【实时节点电价查询】
   * 全网所有节点实时出清节点电价-正式
   * D - 1
   * ACT_NODE_PRICE_INFO = "act_node_price_info",
   */
  
const nodesData:NodeMsg[] =[
  {cityName:'滨州', nodeName:'山东.袁家站/220kV.#1母线', nodeId: 'nodeid_115404740451371486'},
  {cityName:'滨州', nodeName:'山东.双庙站/220kV.#1母线', nodeId: 'nodeid_115404740451369215'},
  {cityName:'滨州', nodeName:'山东.海丰站/220kV.#2母线', nodeId: 'nodeid_115404740451371439'},
  {cityName:'滨州', nodeName:'山东.惠民站/500kV.#2母线', nodeId: 'nodeid_115404740451371772'},
  {cityName:'滨州', nodeName:'山东.官庄站/220kV.#2母线', nodeId: 'nodeid_115404740451370742'},
  {cityName:'滨州', nodeName:'山东.滨州站/500kV.#1母线', nodeId: 'nodeid_115404740451369037'},
  {cityName:'滨州', nodeName:'山东.富国站/500kV.#1母线', nodeId: 'nodeid_115404740451371426'},
  
  {cityName:'德州', nodeName:'山东.赵庄站/220kV.#1母线', nodeId: 'nodeid_115404740451369236'},
  {cityName:'德州', nodeName:'山东.夏津站/220kV.#1母线', nodeId: 'nodeid_115404740451370532'},
  {cityName:'德州', nodeName:'山东.武城站/220kV.#1母线', nodeId: 'nodeid_115404740451369373'},
  {cityName:'德州', nodeName:'山东.守义站/220kV.#1母线', nodeId: 'nodeid_115404740451371914'},
  {cityName:'德州', nodeName:'山东.庆云站/220kV.#1母线', nodeId: 'nodeid_115404740451369259'},
  {cityName:'德州', nodeName:'山东.齐河站/500kV.#1母线', nodeId: 'nodeid_115404740451372856'},
  {cityName:'德州', nodeName:'山东.陵县站/500kV.#2母线', nodeId: 'nodeid_115404740451370091'},
  {cityName:'德州', nodeName:'山东.乐陵站/220kV.#2母线', nodeId: 'nodeid_115404740451369262'},
  {cityName:'德州', nodeName:'山东.华德电厂/500kV.#1母线', nodeId: 'nodeid_115404740451369305'},
  {cityName:'德州', nodeName:'山东.关家三峡储能/220kV.#1母线', nodeId: 'nodeid_115404740451372483'},
  {cityName:'德州', nodeName:'山东.德州站/500kV.#2母线', nodeId: 'nodeid_115404740451369016'},
  {cityName:'德州', nodeName:'山东.贝州站/500kV.#1母线', nodeId: 'nodeid_115404740451371975'},
  
  {cityName:'东营', nodeName:'山东.裕民站/220kV.#1B母线', nodeId: 'nodeid_115404740451371354'},
  {cityName:'东营', nodeName:'山东.学堂站/220kV.#1母线', nodeId: 'nodeid_115404740451369398'},
  {cityName:'东营', nodeName:'山东.利津站/220kV.#1母线', nodeId: 'nodeid_115404740451369427'},
  {cityName:'东营', nodeName:'山东.垦东站/500kV.#1母线', nodeId: 'nodeid_115404740451371862'},
  {cityName:'东营', nodeName:'山东.海口站/500kV.#1母线', nodeId: 'nodeid_115404740451370795'},
  {cityName:'东营', nodeName:'山东.东营电厂/500kV.#1母线', nodeId: 'nodeid_115404740451372177'},
  
  {cityName:'菏泽', nodeName:'山东.郓城站/500kV.#2母线', nodeId: 'nodeid_115404740451370022'},
  {cityName:'菏泽', nodeName:'山东.永丰站/220kV.#1A母线', nodeId: 'nodeid_115404740451372411'},
  {cityName:'菏泽', nodeName:'山东.信义站/220kV.#2母线', nodeId: 'nodeid_115404740451369480'},
  {cityName:'菏泽', nodeName:'山东.润泽电厂/500kV.#2母线', nodeId: 'nodeid_115404740451370196'},
  {cityName:'菏泽', nodeName:'山东.桂陵站/500kV.#1母线', nodeId: 'nodeid_115404740451371902'},
  {cityName:'菏泽', nodeName:'山东.东明站/220kV.#2母线', nodeId: 'nodeid_115404740451369489'},
  {cityName:'菏泽', nodeName:'山东.曹州站/500kV.#1A母线', nodeId: 'nodeid_115404740451371991'},
  {cityName:'菏泽', nodeName:'华北.曹州/1000kV.1M母线', nodeId: 'nodeid_115404740451371989'},
  
  {cityName:'华北网', nodeName:'外网.邢台站/500kV.Ⅰ母线', nodeId: 'nodeid_115404740451371490'},
  {cityName:'华北网', nodeName:'外网.邢台站/1000kV.#1M母线', nodeId: 'nodeid_115404740451371488'},
  {cityName:'华北网', nodeName:'华北.辛安站/500kV.#1母线', nodeId: 'nodeid_115404740451369242'},
  {cityName:'华北网', nodeName:'华北.黄骅/500kV.#2母线', nodeId: 'nodeid_115404740451369987'},
  {cityName:'华北网', nodeName:'华北.海河/500kV.2甲母线', nodeId: 'nodeid_115404740451371369'},
  {cityName:'华北网', nodeName:'华北.海河/500kV.1乙母线', nodeId: 'nodeid_115404740451371370'},
  {cityName:'华北网', nodeName:'华北.海河/1000kV.1母线', nodeId: 'nodeid_115404740451371367'},
  
  {cityName:'华北网外层', nodeName:'华北.宗州/500kV.#2母线', nodeId: 'nodeid_115404740451370642'},
  {cityName:'华北网外层', nodeName:'华北.武邑/500kV.#2母线', nodeId: 'nodeid_115404740451370007'},
  {cityName:'华北网外层', nodeName:'华北.沧西/500kV.#2母线', nodeId: 'nodeid_115404740451370005'},
  
  {cityName:'济南', nodeName:'山东.章丘站/220kV.#1母线', nodeId: 'nodeid_115404740451370453'},
  {cityName:'济南', nodeName:'山东.闻韶站/500kV.#2母线', nodeId: 'nodeid_115404740451369989'},
  {cityName:'济南', nodeName:'山东.天衍站/500kV.#1母线', nodeId: 'nodeid_115404740451370684'},
  {cityName:'济南', nodeName:'山东.泉城站/500kV.#1A母线', nodeId: 'nodeid_115404740451370942'},
  {cityName:'济南', nodeName:'山东.蟠龙站/500kV.#1母线', nodeId: 'nodeid_115404740451370664'},
  {cityName:'济南', nodeName:'山东.济南站/500kV.#2母线', nodeId: 'nodeid_115404740451369177'},
  {cityName:'济南', nodeName:'山东.长清站/500kV.#1母线', nodeId: 'nodeid_115404740451369030'},
  {cityName:'济南', nodeName:'华北.泉城/1000kV.1M母线', nodeId: 'nodeid_115404740451370925'},
  
  {cityName:'济宁', nodeName:'山东.邹县电厂/500kV.#1母线', nodeId: 'nodeid_115404740451369005'},
  {cityName:'济宁', nodeName:'山东.运河电厂/500kV.#1母线', nodeId: 'nodeid_115404740451369536'},
  {cityName:'济宁', nodeName:'山东.儒林站/500kV.#1母线', nodeId: 'nodeid_115404740451371885'},
  {cityName:'济宁', nodeName:'山东.麟祥站/500kV.#2母线', nodeId: 'nodeid_115404740451372985'},
  {cityName:'济宁', nodeName:'山东.金多站/500kV.#1母线', nodeId: 'nodeid_115404740451371358'},
  {cityName:'济宁', nodeName:'山东.嘉祥电厂/500kV.#2母线', nodeId: 'nodeid_115404740451369097'},
  
  {cityName:'莱芜', nodeName:'山东.鲁中站/500kV.#2母线', nodeId: 'nodeid_115404740451369022'},
  {cityName:'莱芜', nodeName:'山东.莱芜电厂/500kV.#1母线', nodeId: 'nodeid_115404740451370844'},
  {cityName:'莱芜', nodeName:'山东.北赵站/220kV.#2母线', nodeId: 'nodeid_115404740451370476'},
  
  {cityName:'聊城', nodeName:'山东.武阳站/500kV.#2母线', nodeId: 'nodeid_115404740451372370'},
  {cityName:'聊城', nodeName:'山东.聊城站/500kV.#2母线', nodeId: 'nodeid_115404740451368991'},
  {cityName:'聊城', nodeName:'山东.聊城电厂/500kV.#1母线', nodeId: 'nodeid_115404740451369192'},
  {cityName:'聊城', nodeName:'山东.乐平站/500kV.#1母线', nodeId: 'nodeid_115404740451371833'},
  {cityName:'聊城', nodeName:'山东.柴府站/500kV.#1母线', nodeId: 'nodeid_115404740451370913'},
  
  {cityName:'临沂', nodeName:'山东.沂蒙站/500kV.#2母线', nodeId: 'nodeid_115404740451369113'},
  {cityName:'临沂', nodeName:'山东.郯城站/220kV.#1母线', nodeId: 'nodeid_115404740451369829'},
  {cityName:'临沂', nodeName:'山东.沭河站/500kV.#1母线', nodeId: 'nodeid_115404740451372425'},
  {cityName:'临沂', nodeName:'山东.兰陵站/500kV.#1母线', nodeId: 'nodeid_115404740451370897'},
  {cityName:'临沂', nodeName:'山东.高乡站/500kV.#2A母线', nodeId: 'nodeid_115404740451371664'},
  {cityName:'临沂', nodeName:'山东.兵房国投储能电站/220kV.Ⅰ段母线', nodeId: 'nodeid_115404740451372789'},
  {cityName:'临沂', nodeName:'华北.高乡/1000kV.2M母线', nodeId: 'nodeid_115404740451371668'},
  {cityName:'临沂', nodeName:'国调.沂南换流站/500kV.#1M母线', nodeId: 'nodeid_115404740451371603'},
  {cityName:'临沂', nodeName:'国调.沂南换流站/1000kV.#1M母线', nodeId: 'nodeid_115404740451371605'},
  {cityName:'临沂', nodeName:'山东.归昌中核/220kV.#1母线', nodeId: 'nodeid_115404740451372728'},
  
  {cityName:'青岛', nodeName:'山东.珠山站/220kV.#2母线', nodeId: 'nodeid_115404740451369750'},
  {cityName:'青岛', nodeName:'山东.神山站/500kV.#1母线', nodeId: 'nodeid_115404740451371936'},
  {cityName:'青岛', nodeName:'山东.崂山站/500kV.#1母线', nodeId: 'nodeid_115404740451369020'},
  {cityName:'青岛', nodeName:'山东.琅琊站/500kV.#1母线', nodeId: 'nodeid_115404740451369008'},
  {cityName:'青岛', nodeName:'山东.观龙站/500kV.#1母线', nodeId: 'nodeid_115404740451372314'},
  {cityName:'青岛', nodeName:'山东.大泽站/500kV.#1母线', nodeId: 'nodeid_115404740451370129'},
  {cityName:'青岛', nodeName:'国调.胶东换流站/500kV.M63母线', nodeId: 'nodeid_115404740451370743'},
  
  {cityName:'日照', nodeName:'山东.日照站/500kV.#2母线', nodeId: 'nodeid_115404740451369043'},
  {cityName:'日照', nodeName:'山东.巨峰站/500kV.#1母线', nodeId: 'nodeid_115404740451370846'},
  {cityName:'日照', nodeName:'山东.安东站/220kV.#1母线', nodeId: 'nodeid_115404740451369739'},
  
  {cityName:'泰安', nodeName:'山东.新泰站/500kV.#1母线', nodeId: 'nodeid_115404740451371384'},
  {cityName:'泰安', nodeName:'山东.桃都中储储能/220kV.#1母线', nodeId: 'nodeid_115404740451373038'},
  {cityName:'泰安', nodeName:'山东.泰山站/500kV.#2母线', nodeId: 'nodeid_115404740451369165'},
  {cityName:'泰安', nodeName:'山东.红庙站/220kV.#2母线', nodeId: 'nodeid_115404740451369827'},
  
  {cityName:'威海', nodeName:'山东.文登电站/500kV.#1母线', nodeId: 'nodeid_115404740451372571'},
  {cityName:'威海', nodeName:'山东.荣成站/220kV.#1母线', nodeId: 'nodeid_115404740451369878'},
  {cityName:'威海', nodeName:'山东.杜家站/220kV.#2A母线', nodeId: 'nodeid_115404740451370352'},
  
  {cityName:'潍坊', nodeName:'山东.摘月站/220kV.#2母线', nodeId: 'nodeid_115404740451371973'},
  {cityName:'潍坊', nodeName:'山东.益都站/500kV.#1母线', nodeId: 'nodeid_115404740451369059'},
  {cityName:'潍坊', nodeName:'山东.羊口电厂/500kV.#1母线', nodeId: 'nodeid_115404740451370877'},
  {cityName:'潍坊', nodeName:'山东.潍坊站/500kV.#2母线', nodeId: 'nodeid_115404740451368973'},
  {cityName:'潍坊', nodeName:'山东.寿光站/500kV.#1母线', nodeId: 'nodeid_115404740451370048'},
  {cityName:'潍坊', nodeName:'山东.临朐站/500kV.#2母线', nodeId: 'nodeid_115404740451371874'},
  {cityName:'潍坊', nodeName:'山东.官亭站/500kV.#1母线', nodeId: 'nodeid_115404740451372072'},
  {cityName:'潍坊', nodeName:'山东.昌乐站/500kV.#1B母线', nodeId: 'nodeid_115404740451371466'},
  {cityName:'潍坊', nodeName:'山东.宝石站/500kV.#1母线', nodeId: 'nodeid_115404740451372180'},
  {cityName:'潍坊', nodeName:'山东.安丘站/220kV.#2母线', nodeId: 'nodeid_115404740451369737'},
  {cityName:'潍坊', nodeName:'国调.广固换流站/500kV.#1母线', nodeId: 'nodeid_115404740451371627'},
  {cityName:'潍坊', nodeName:'华北.昌乐/1000kV.1M母线', nodeId: 'nodeid_115404740451371468'},
  {cityName:'潍坊', nodeName:'山东.泊南中核储能/220kV.Ⅰ段母线', nodeId: 'nodeid_115404740451373220'},
  {cityName:'潍坊', nodeName:'山东.寒亭站/220kV.#1母线', nodeId: 'nodeid_115404740451369760'},
  
  {cityName:'西北网', nodeName:'国调.银川东换流站/330kV.2母线', nodeId: 'nodeid_115404740451370203'},
  
  {cityName:'烟台', nodeName:'山东.芝罘站/220kV.#2母线', nodeId: 'nodeid_115404740451369533'},
  {cityName:'烟台', nodeName:'山东.栖霞站/500kV.#2母线', nodeId: 'nodeid_115404740451370061'},
  {cityName:'烟台', nodeName:'山东.罗峰站/500kV.500kV#1母线', nodeId: 'nodeid_115404740451372837'},
  {cityName:'烟台', nodeName:'山东.莱州电厂/500kV.#2B母线', nodeId: 'nodeid_115404740451371854'},
  {cityName:'烟台', nodeName:'山东.莱阳站/500kV.#1母线', nodeId: 'nodeid_115404740451369079'},
  {cityName:'烟台', nodeName:'山东.海阳核电/500kV.#2B母线', nodeId: 'nodeid_115404740451370824'},
  {cityName:'烟台', nodeName:'山东.光州站/500kV.#1母线', nodeId: 'nodeid_115404740451370050'},
  {cityName:'烟台', nodeName:'山东.福山站/220kV.#2母线', nodeId: 'nodeid_115404740451369616'},
  {cityName:'烟台', nodeName:'山东.登州站/500kV.500kV#1B母线', nodeId: 'nodeid_115404740451372870'},
  {cityName:'烟台', nodeName:'山东.(新)掖县站/220kV.#2母线', nodeId: 'nodeid_115404740451371695'},
  {cityName:'烟台', nodeName:'山东.北马站/220kV.#2母线', nodeId: 'nodeid_115404740451370332'},
  
  {cityName:'枣庄', nodeName:'山东.枣庄站/500kV.#1母线', nodeId: 'nodeid_115404740451369112'},
  {cityName:'枣庄', nodeName:'山东.微山湖站/500kV.#1A母线', nodeId: 'nodeid_115404740451372000'},
  {cityName:'枣庄', nodeName:'山东.山亭中广场/220kV.#1母线', nodeId: 'nodeid_115404740451370807'},
  {cityName:'枣庄', nodeName:'山东.匡衡站/500kV.#2母线', nodeId: 'nodeid_115404740451371754'},
  {cityName:'枣庄', nodeName:'山东.文峰站/220kV.#2母线', nodeId: 'nodeid_115404740451369835'},
  {cityName:'枣庄', nodeName:'山东.丰泽站/220kV.#1母线', nodeId: 'nodeid_115404740451370230'},
  {cityName:'枣庄', nodeName:'华北.微山湖/1000kV.1M母线', nodeId: 'nodeid_115404740451371998'},

  {cityName:'淄博', nodeName:'山东.沂源站/220kV.#1母线', nodeId: 'nodeid_115404740451369843'}
]

export const dataTypeList:{value:DataSourceTypeEnum, label:string}[] = [
  {
    value: DataSourceTypeEnum.AHEAD_NODE_PRICE_INFO,
    label: '日前出清节点电价 D+1'
  },
  {
    value: DataSourceTypeEnum.ACT_NODE_PRICE_INFO,
    label: '实时出清节点电价-正式 d-1'
  }
]

type GetOneNodeDataParams = {
  headers: HeadersInit,
  reqType:DataSourceTypeEnum, 
  nodeMsg:NodeMsg,
  pdate: string
}
type YunNaoSaveData = {
  runDate: string,
  nodeId: string,
  cityName: string,
  nodeName: string,
  nodePrice: string | number
}[]

type OneNodeOriginData = {
  Val: string[]
}

/** 处理单个节点的数据结构 */
const originNodeDataToYunNao = (originData:OneNodeOriginData, params:GetOneNodeDataParams):YunNaoSaveData => {
  const originNooePriceList = originData?.Val || []
  const {pdate, nodeMsg} = params
  // 获取 96 点数据
 const timeArr96 = createSerialDates([pdate, pdate],96,'HH:mm')

 return timeArr96.map((time, index) => ({
  runDate:`${pdate} ${time}`,
  oriRunDate:`${pdate} ${sdTime96[index]}`,
  ...nodeMsg,
  nodePrice: originNooePriceList[index] ?? null
 }))
}

const replaceNodeId = async (nodeId:string) => {
  const replaceNodeIds = await getReplaceNodeIdsData() as [string,string][]

  const oldNewId = replaceNodeIds.find((val:[string,string]) => val[0] === nodeId)

  if(oldNewId) return Promise.resolve(oldNewId[1])
 return Promise.resolve(nodeId)
}

/**
 * 获取一个节点的数据
 * @param params GetOneNodeDataParams
 * @returns 
 */
const getOneNodeData = async (params:GetOneNodeDataParams):Promise<YunNaoSaveData> => {
  const {reqType, nodeMsg:{nodeId}, headers} = params
  const unitid = await replaceNodeId(nodeId)
  const data:OneNodeOriginData = await spotMarketBatch?.[reqType]?.({headers, unitid, pdate:'' })
  const yunNaoSaveData = originNodeDataToYunNao(data, params)
  return Promise.resolve(yunNaoSaveData)
}

/**
 * 获取120个节点的数据
 * @param ruleForm RuleForm
 * @param headersInfo HeadersInit
 * @param isHandle 1. false: 获取120个节点的数据，在getOneNodeData中自动替换; 2. true: 只获取替换节点的数据，在getOneNodeData中自动替换
 * @returns 
 */
export const getAllNodeData = async (ruleForm:GetAllNodeDataParams, headersInfo?:HeadersInit, isHandle = false) => {
  try {
    let reqInfo = {headers:headersInfo}
    if(!headersInfo) reqInfo = await getSpotMartetReqInfo(true) as { headers: HeadersInit; }
    if(!reqInfo?.headers) return Promise.reject(new Error('getAllNodeData  headers'))
    const headers = reqInfo.headers
  
    const {reqType, certainDate} = ruleForm

    // 设置数据日期
    await spotMarketCommon?.sdSpotMarketChangeDate?.(certainDate)

    const replaceNodeData = await getReplaceNodeData(isHandle)

    const reqParamsList:GetOneNodeDataParams[] = replaceNodeData.map(val => ({headers,reqType,nodeMsg: val, pdate: certainDate}))
  
    const allNodeData:CbParams = await creatAsyncRecursion(AsyncRecursion, {
      stepAsyncFnParamsList: reqParamsList,
      stepAsyncFn: getOneNodeData,
      stepCb: async () => await promisetTimeOut(50), 
      stepAsyncNum: 1,
      errBreak: false
    }).start()

    console.log('allNodeData', allNodeData)
    return Promise.resolve({
      dataType: ruleForm.reqType,
      dataSource:(allNodeData?.allResultList || []).flat(),
      date: certainDate
    })
  }catch(e) {
    console.error(e)
  }
}

/**
 * 调用云脑接口存储数据  通过云脑接口调用数据组数据，最终将数据存储到数组组数据库
 */
const saveWebSpiderData = async (type: string) => {
  /**没有爬取时间的不往下执行 */
  if (!sendParam[type].data[0].business_time) return;

  try {
    const saveData = JSON.parse(JSON.stringify(sendParam[type]))

    /**置空已调用存储接口的类型数据，重新开始爬取存储 */
    console.log('存储原数据：', JSON.parse(JSON.stringify(sendParam[type].data)))
    sendParam[type].data = getDefaultDataBaseData()

    const {haveData, saveDataFilter} = sendParamFilterData(saveData.data[0].data)
    if(!haveData) {
      console.log('无数据，不存')
      return Promise.resolve()
    }
    saveData.data[0].data = saveDataFilter

    console.log('存储数据：', saveData)
    await yunNao.datapreservation([saveData]);
    console.log("获取数据存储成功", dayjs().format("YYYY-MM-DD HH:mm:ss"));
  } catch (error) {
    console.error("获取数据存储失败: ", error);
  }
}

/** 调用云脑接口存储的数据结构 */
let sendParam: { [key: string]: any } = cloneDeep(sendParamsStructure);
export const yunNaoSaveNodeData = async (formatData: any) => {
  const typeKey = formatData.dataType
  const saveData = formatData.dataSource
  if (dataCategory[DataSourceTypeByDateEnum.D_REDUCE_DATE].includes(typeKey)) {
    Object.assign(
      sendParam[DataSourceTypeByDateEnum.D_REDUCE_DATE].data[0].data,
      { [typeKey]: saveData }
    );
    sendParam[DataSourceTypeByDateEnum.D_REDUCE_DATE].data[0].business_time = formatData.date;
    await saveWebSpiderData(DataSourceTypeByDateEnum.D_REDUCE_DATE);
  } else if (dataCategory[DataSourceTypeByDateEnum.D_DATE].includes(typeKey)) {
    Object.assign(
      sendParam[DataSourceTypeByDateEnum.D_DATE].data[0].data,
      { [typeKey]: saveData }
    );
    sendParam[DataSourceTypeByDateEnum.D_DATE].data[0].business_time = formatData.date;
    await saveWebSpiderData(DataSourceTypeByDateEnum.D_DATE);
  } else if (dataCategory.d_add_date.includes(typeKey)) {
    Object.assign(
      sendParam[DataSourceTypeByDateEnum.D_ADD_DATE].data[0].data,
      { [typeKey]: saveData }
    );
    sendParam[DataSourceTypeByDateEnum.D_ADD_DATE].data[0].business_time = formatData.date;
    await saveWebSpiderData(DataSourceTypeByDateEnum.D_ADD_DATE);
  }
};